/*
 * @(#)XfeditRulebaseTableRenderer.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfedit;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import javax.swing.*;
import java.awt.*;

/**
 * Clase que describe el aspecto de los elementos en la representaci�n
 * tabular de las bases de reglas. Tiene como finalidad transformar
 * el valor seleccionado en el combobox en el valor mostrado en la celda
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfeditRulebaseTableRenderer implements ListCellRenderer {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Modelo de la tabla
	 */
	private XfeditRulebaseTableModel model;
	
	/**
	 * �ndice de la variable asociada a la celda
	 */
	private int i;
	
	/**
	 * Permite distinguir entre celdas de entrada y de salida
	 */
	private boolean inputcombo;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfeditRulebaseTableRenderer(Object model, int i, boolean inputcombo) {
		this.model = (XfeditRulebaseTableModel) model;
		this.i = i;
		this.inputcombo = inputcombo;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Interfaz ListCellRenderer
	 */
	public Component getListCellRendererComponent( JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		LinguisticLabel pmf = null;
		try { pmf = (LinguisticLabel) value; } catch(Exception ex) {}
		String label;
		if(index == -1 && pmf != null && inputcombo)
			label = model.getInput(i)+" == "+pmf;
		else if(index == -1 && pmf !=null && !inputcombo)
			label = model.getOutput(i)+" = "+pmf;
		else if(pmf == null) label = " ";
		else label = pmf.toString();
		JLabel renderer = new JLabel(label);
		renderer.setForeground(Color.black);
		if(isSelected) renderer.setBackground(XConstants.textselectedbg);
		else renderer.setBackground(XConstants.textbackground);
		renderer.setOpaque(true);
		renderer.setFont(XConstants.textfont);
		return renderer;
	}
}

